<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 
 * @package     ceo
 * @subpackage  models
 * @category    models
 * @author      ydd team, wu
 * @copyright   copyright (c) 2012, yaoduoduo, Inc.
 * @filesource
 */

class Demand_model extends Base_model {
    protected $_table_name = 'demand';
    protected $_key_name = 'demand_id';
	
	function get_demand_info($id) {
		if(!empty($id))
			$where[$this->_key_name]=$id;
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where['dtime']=0;
		return $this->fetch(NULL, $where,NULL,'row');
	}

	function get_demand_name($name) {
		if(!empty($name))
			$where["name"]=$name;
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where['dtime']=0;
		return $this->fetch(NULL, $where,NULL,'row');
	}

	function get_demand_name_count($name) {
		if($name!="")
			$where["name"]=$name;
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where['dtime']=0;
		return $this->fetch(NULL, $where,NULL,'count');
	}

    function get_demand($uri_query,$limit) {
		$this->set_limit($limit);
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where=array('dtime'=>0,'name'=>$uri_query['keyword']);
		return $this->fetch(NULL, $where);
    }
	function get_demand_count($uri_query) {
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$con["where"][] = array('dtime'=>0,'name'=>$uri_query['keyword']);
        $this->set_args_array($con);
        return $this->fetch_count();
	}
	function get_all() {
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where=array('dtime'=>0);
		return $this->fetch(NULL, $where);
    }
	
	function demand_pair($where=null,$w=null,$limit){
		$this->set_limit($limit);
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where['dtime']=0;
		return $this->fetch(NULL, $where);	
	}
	function demand_pair_count($where=null,$w=null){
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where['dtime'] =0;
		return $this-> fetch(NULL, $where, NULL,"count");	
	}


	//配對 
	function match_demand($where){

		$this->db->where($where);
		$this->db->order_by("ctime", "DESC"); 

		$query = $this->db->get("h_demand");
		return $query;
	}

	//查詢
	function query_demand($w=null,$keyword=null,$limit) 
	{
		$this->db->order_by("ctime", "DESC"); 
		$this->set_limit($limit);
		if($keyword!="")
			$this->set_or_like('name',$keyword);
		if(isset($w['name']))
			$this->set_or_like('name',$w['name']);
		if(isset($w['required_level']))
			$where['required_level'] =$w['required_level'];
		if(isset($w['type_rent']))
			$this->db->or_like('type_rent', $w['type_rent']); 
		if(isset($w['type_sell']))
			$this->db->or_like('type_sell', $w['type_sell']); 

		//if(isset($w['type_consignment'])){

			//$this->db->where('demand_id', '32'); 
			
		//}
			
		if(isset($w['object_usage']))
			$where['object_usage'] =$w['object_usage'];
		if(isset($w['building_area1']))
			$where['building_area1 >='] =$w['building_area1'];
		if(isset($w['building_area2']))
			$where['building_area2 <='] =$w['building_area2'];
		if(isset($w['indoor_area1']))
			$where['indoor_area1 >='] =$w['indoor_area1'];
		if(isset($w['indoor_area2']))
			$where['indoor_area2 <='] =$w['indoor_area2'];
		if(isset($w['land_area1']))
			$where['land_area1 >='] =$w['land_area1'];
		if(isset($w['land_area2']))
			$where['land_area2 <='] =$w['land_area2'];
		if(isset($w['total_budget1']))
			$where['total_budget1 >='] =$w['total_budget1'];
		if(isset($w['total_budget2']))
			$where['total_budget2 <='] =$w['total_budget2'];
		//fix by vicotr
		if(isset($w['key_areas1']))
			$where['key_areas1'] =$w['key_areas1'];
		if(isset($w['key_areas2']))
			$where['key_areas2'] =$w['key_areas2'];
		//fix by victor

		if(isset($w['key_areas']))
			$this->set_or_like('key_areas1,key_areas2',$w['key_areas']);

		if(isset($w['specify_building1']))
			$this->set_or_like('specify_building1',$w['specify_building1']);
		if(isset($w['specify_building2']))
			$this->set_or_like('specify_building2',$w['specify_building2']);

		if(isset($w['object_name']))
			$this->set_or_like('object_name1,object_name2',$w['object_name']);
		if(isset($w['sections1']))
			$this->set_or_like('sections1,sections2',$w['sections1']);
		if(isset($w['sections2']))
			$this->set_or_like('sections1,sections2',$w['sections2']);
		if(isset($w['administrative_area']))
			$this->set_or_like('administrative_area1,administrative_area2',$w['administrative_area']);
		if(isset($w['year_of_construction1']))
			$where['year_of_construction >='] =$w['year_of_construction1'];
		if(isset($w['year_of_construction2']))
			$where['year_of_construction <='] =$w['year_of_construction2'];
		if(isset($w['parking_number']))
			$where['parking_number >='] =$w['parking_number'];
		if(isset($w['status']))
			$where['status'] =$w['status'];
		if(isset($w['customer_source']))
			$where['customer_source'] =$w['customer_source'];

		if(isset($w['sctime'])){
			$ts=explode("/",$w['sctime']);
			$where['ctime >='] =mktime(0,0,0,$ts[1],$ts[2],$ts[0]);
		}
		if(isset($w['ectime'])){
			$te=explode("/",$w['ectime']);
			$where['ctime <='] =mktime(23,59,59,$te[1],$te[2],$te[0]);
		}
		if(isset($w['broker']))
			$this->set_or_like('broker',$w['broker']);
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}

		if(!empty($w['type_consignment']) ){

		//	echo "thanks~~~dah omm";

			$where['type_consignment >='] =$w['type_consignment'];

		}
		$where['dtime'] =0;
		//print_r($where);
		return $this->fetch(NULL, $where);
    }

	function query_demand_count($w=null,$keyword=nulls)
	{
		if($keyword!="")
			$this->set_or_like('name',$keyword);
		if(isset($w['name']))
			$this->set_or_like('name',$w['name']);
		if(isset($w['required_level']))
			$where['required_level'] =$w['required_level'];
		if(isset($w['type']))
			$where['type'] =$w['type'];
		if(isset($w['object_usage']))
			$where['object_usage'] =$w['object_usage'];
		if(isset($w['building_area1']))
			$where['building_area1 >='] =$w['building_area1'];
		if(isset($w['building_area2']))
			$where['building_area2 <='] =$w['building_area2'];
		if(isset($w['indoor_area1']))
			$where['indoor_area1 >='] =$w['indoor_area1'];
		if(isset($w['indoor_area2']))
			$where['indoor_area2 <='] =$w['indoor_area2'];
		if(isset($w['land_area1']))
			$where['land_area1 >='] =$w['land_area1'];
		if(isset($w['land_area2']))
			$where['land_area2 <='] =$w['land_area2'];
		if(isset($w['total_budget1']))
			$where['total_budget1 >='] =$w['total_budget1'];
		if(isset($w['total_budget2']))
			$where['total_budget2 <='] =$w['total_budget2'];
		if(isset($w['key_areas']))
			$this->set_or_like('key_areas1,key_areas2',$w['key_areas']);

		if(isset($w['specify_building']))
			$this->set_or_like('specify_building1,specify_building2',$w['specify_building']);
		if(isset($w['object_name1']))
			$this->set_or_like('object_name11,object_name12',$w['object_name1']);
		if(isset($w['sections']))
			$this->set_or_like('sections1,sections2',$w['sections']);
		if(isset($w['administrative_area']))
			$this->set_or_like('administrative_area1,administrative_area2',$w['administrative_area']);
		if(isset($w['year_of_construction1']))
			$where['year_of_construction >='] =$w['year_of_construction1'];
		if(isset($w['year_of_construction2']))
			$where['year_of_construction <='] =$w['year_of_construction2'];
		if(isset($w['status']))
			$where['status'] =$w['status'];
		if(isset($w['customer_source']))
			$where['customer_source'] =$w['customer_source'];

		if(isset($w['sctime'])){
			$ts=explode("/",$w['sctime']);
			$where['ctime >='] =mktime(0,0,0,$ts[1],$ts[2],$ts[0]);
		}
		if(isset($w['ectime'])){
			$te=explode("/",$w['ectime']);
			$where['ctime <='] =mktime(23,59,59,$te[1],$te[2],$te[0]);
		}
		if(isset($w['broker']))
			$this->set_or_like('broker',$w['broker']);
		$user_purview=check_broker_is_visible();
		if(!$user_purview){
			$this->set_or_like('front_man,broker',$this->login_lib->users_name());
		}
		$where['dtime'] =0;
		return $this-> fetch(NULL, $where, NULL,"count");
	}
	
	function update_demand($data,$id) {
		$where[]=array('demand_id'=>$id);
		return $this->update($data, $where);
	}

	function add_demand($data) {		
		$i="";
		if($data['type_rent']=="1")
			$i=$this->add_demand_rent($data);
		if($data['type_sell']=="1")
			$i=$this->add_demand_sell($data);
		return $i;
	}
	function add_demand_rent($data) {
		foreach($data as $key=>$val){
			if($key!="reported_rate1" && $key!="reported_rate2" && $key!="type_sell" && $key!="type_consignment")
				$dr[$key]=$val;
		}
		$dr['shop_point_id']=$this->login_lib->shop_point_id();
		$dr['ctime']=time();
		//print_r($dr);die();
		return $this->insert ( $dr );
	}
	function add_demand_sell($data) {
		foreach($data as $key=>$val){
			if($key!="leasing_business_purposes" && $key!="type_rent" && $key!="type_consignment" )
				$ds[$key]=$val;
		}
		$ds['shop_point_id']=$this->login_lib->shop_point_id();
		$ds['ctime']=time();
		return $this->insert ( $ds );
	}
	function add_demand_consignment($data) {

		$data['shop_point_id']=$this->login_lib->shop_point_id();
		$data['ctime']=time();
		return $this->insert ( $data );

	}
	function del_demand($data) {
		$where[]=array('demand_id'=>$data['id']);
		$value=array('dtime'=>time());
		return $this->update($value, $where);
	}
	
}